#!/bin/bash

# == Simple If Statement == 
# syntax: 
#   if (conditional-expression)
#       action

# syntax: 
#   if (conditional-expression)
#   {
#       action1;
#       action2;
#   }

# - print all the items with quantity <= 5
awk -f ./06_if_statment.awk ./test_items.txt
echo -e '\n' 



# == If-else Statment == 
# syntax: 
#   if (conditional-expression)
#       action1
#   else 
#       action2
# or:
# conditional-expression ? action1 : action2;
awk -f ./06_if_else.awk ./test_items.txt
echo -e '\n'



# == While loop ==
# syntax: 
#   while(condition)
#       actions
# NOTE: 
#   1. while is awk keyword
#   2. actions is body of while loop, if there are more than one action, 
#      the actions must be enclosed by {}
#   3. Do-While is identical to C/C++ one
awk -f ./06_while_loop.awk ./test_items-sold.txt
echo -e '\n'



# == For loop ==
# syntax:
#   for(init; condition; steps)
#       actions
# the syntax of awk for-loop is identical to C/C++ one

echo "1 2 3 4" | awk \
    '
    BEGIN{
        total=0
    }
    {
        for (i = 1; i <= NF; i++)
            total=total + $i;
    }
    END {print total}
    '
echo -e '\n'

# NOTE: the continue, break statement is also identical to C/C++ ones


# == Exit statement == 
# the exit statement causes the scrpt to immediately stop executing 
# the current commands, and also ignores the remaining lines from the input file
awk '
BEGIN {
    x=1;
    while(x <= 10)
    {
        if(x==5)
            exit;
        print "Value of x", x;
        x++;
    }
}
'
